/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package action.activity;

/**
 *
 * @author HX
 */
import com.opensymphony.xwork2.ActionContext;
import java.io.IOException;

import com.opensymphony.xwork2.ActionSupport;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletResponseAware;
import po.User;
import service.ActivityService;
import service.OrgService;

public class EditBehaviorAction extends ActionSupport implements ServletResponseAware{
    private HttpServletResponse response;
    private OrgService orgService;
    private ActivityService activityService;
    private HttpServletRequest request;
    private int activityId;
    private int orgId;
    private char degree;
    private String behavior;
    private String actorIdStr;

    @Override
    public String execute() {
        try {
            int userId;
            String msg;
            int actorId = Integer.parseInt((String)actorIdStr.substring(2, actorIdStr.length()));
            if (ActionContext.getContext().getSession().get("user") == null) {
                msg = "you should login first";
                request.setAttribute("msg", msg);
                return ERROR;
             }
            userId = ((User) ActionContext.getContext().getSession().get("user")).getId();
            if(!orgService.isLeader(userId, orgId)){
                msg = "you can not score!";
                request.setAttribute("msg", msg);
                return ERROR;
            }
            activityService.editActivityBehavior(activityId, actorId, behavior);

            response.setContentType("application/json; charset=utf-8;");
            PrintWriter out = response.getWriter();
            out.println(behavior);
            out.flush();
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(EditBehaviorAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return SUCCESS;
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    public OrgService getOrgService() {
        return orgService;
    }

    public void setOrgService(OrgService orgService) {
        this.orgService = orgService;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public char getDegree() {
        return degree;
    }

    public void setDegree(char degree) {
        this.degree = degree;
    }

    public String getBehavior() {
        return behavior;
    }

    public void setBehavior(String behavior) {
        this.behavior = behavior;
    }


    public int getOrgId() {
        return orgId;
    }

    public void setOrgId(int orgId) {
        this.orgId = orgId;
    }

    public String getActorIdStr() {
        return actorIdStr;
    }

    public void setActorIdStr(String actorIdStr) {
        this.actorIdStr = actorIdStr;
    }

    public ActivityService getActivityService() {
        return activityService;
    }

    public void setActivityService(ActivityService activityService) {
        this.activityService = activityService;
    }


}